package annotation.cal;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collection;

public class TestCal {
    public static void main(String[] args) throws IOException {
        Calculator c = new Calculator();
        Class aClass = c.getClass();
        Method[] methods = aClass.getMethods();
        int count = 0;
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("bug.txt",true));
        for (Method method:methods){
            if (method.isAnnotationPresent(Check.class)) {
                try {
                    method.invoke(c);
                } catch (Exception e) {
                    count++;
                    bufferedWriter.write("异常名称："+e.getCause());
                    bufferedWriter.newLine();
                    bufferedWriter.write("异常原因："+e.getCause().getMessage());
                    bufferedWriter.newLine();
                    bufferedWriter.write("------------------");
                    bufferedWriter.newLine();
                }
            }
        }
        bufferedWriter.write("本次共发生异常："+count+"次");
        bufferedWriter.flush();
        bufferedWriter.close();
    }
}
